﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
    
namespace Seminarska
{
    class TouchScreenKeyboard : Window
    {
        #region Property & Variable & Constructor
        private static double _WidthTouchKeyboard = 963;
        private static double _HeightTouchKeyboard = 340;
       
        public static double WidthTouchKeyboard
        {
            get { return _WidthTouchKeyboard; }
            set { _WidthTouchKeyboard = value; }

        }
        public static double HeightTouchKeyboard
        {
            get { return _HeightTouchKeyboard; }
            set { _HeightTouchKeyboard = value; }

        }
        
        private static bool _ShiftFlag;

        protected static bool ShiftFlag
        {
            get { return _ShiftFlag; }
            set { _ShiftFlag = value; }
        }

        private static bool _CapsLockFlag;

        protected static bool CapsLockFlag
        {
            get { return TouchScreenKeyboard._CapsLockFlag; }
            set { TouchScreenKeyboard._CapsLockFlag = value; }
        }

        private static Window _InstanceObject;

        private static Brush _PreviousTextBoxBackgroundBrush = null;
        private static Brush _PreviousTextBoxBorderBrush = null;
        private static Thickness _PreviousTextBoxBorderThickness;

        private static Control _CurrentControl;
        public static string TouchScreenText
        {
            get
            {
                if (_CurrentControl is TextBox)
                {
                    return ((TextBox)_CurrentControl).Text;
                }
                else if (_CurrentControl is PasswordBox)
                {
                    return ((PasswordBox)_CurrentControl).Password;
                }
                else return "";


            }
            set
            {
                if (_CurrentControl is TextBox)
                {
                    ((TextBox)_CurrentControl).Text = value;
                }
                else if (_CurrentControl is PasswordBox)
                {
                    ((PasswordBox)_CurrentControl).Password = value;
                }


            }

        }

        public static RoutedUICommand CmdTlide = new RoutedUICommand();
        public static RoutedUICommand Cmd1 = new RoutedUICommand();
        public static RoutedUICommand Cmd2 = new RoutedUICommand();
        public static RoutedUICommand Cmd3 = new RoutedUICommand();
        public static RoutedUICommand Cmd4 = new RoutedUICommand();
        public static RoutedUICommand Cmd5 = new RoutedUICommand();
        public static RoutedUICommand Cmd6 = new RoutedUICommand();
        public static RoutedUICommand Cmd7 = new RoutedUICommand();
        public static RoutedUICommand Cmd8 = new RoutedUICommand();
        public static RoutedUICommand Cmd9 = new RoutedUICommand();
        public static RoutedUICommand Cmd0 = new RoutedUICommand();
        public static RoutedUICommand CmdMinus = new RoutedUICommand();
        public static RoutedUICommand CmdPlus = new RoutedUICommand();
        public static RoutedUICommand CmdBackspace = new RoutedUICommand();


        public static RoutedUICommand CmdTab = new RoutedUICommand();
        public static RoutedUICommand CmdЉ = new RoutedUICommand();
        public static RoutedUICommand CmdЊ = new RoutedUICommand();
        public static RoutedUICommand CmdЕ = new RoutedUICommand();
        public static RoutedUICommand CmdР = new RoutedUICommand();
        public static RoutedUICommand CmdТ = new RoutedUICommand();
        public static RoutedUICommand CmdЅ = new RoutedUICommand();
        public static RoutedUICommand CmdУ = new RoutedUICommand();
        public static RoutedUICommand CmdИ = new RoutedUICommand();
        public static RoutedUICommand CmdО = new RoutedUICommand();
        public static RoutedUICommand CmdП = new RoutedUICommand();
        public static RoutedUICommand CmdШ = new RoutedUICommand();
        public static RoutedUICommand CmdЃ = new RoutedUICommand();
        public static RoutedUICommand CmdЖ = new RoutedUICommand();

        public static RoutedUICommand CmdCapsLock = new RoutedUICommand();
        public static RoutedUICommand CmdА = new RoutedUICommand();
        public static RoutedUICommand CmdС = new RoutedUICommand();
        public static RoutedUICommand CmdД = new RoutedUICommand();
        public static RoutedUICommand CmdФ = new RoutedUICommand();
        public static RoutedUICommand CmdГ = new RoutedUICommand();
        public static RoutedUICommand CmdХ = new RoutedUICommand();
        public static RoutedUICommand CmdЈ = new RoutedUICommand();
        public static RoutedUICommand CmdК = new RoutedUICommand();
        public static RoutedUICommand CmdЛ = new RoutedUICommand();
        public static RoutedUICommand CmdЧ = new RoutedUICommand();
        public static RoutedUICommand CmdЌ = new RoutedUICommand();
        public static RoutedUICommand CmdEnter = new RoutedUICommand();

        public static RoutedUICommand CmdShift = new RoutedUICommand();
        public static RoutedUICommand CmdЗ = new RoutedUICommand();
        public static RoutedUICommand CmdЏ = new RoutedUICommand();
        public static RoutedUICommand CmdЦ = new RoutedUICommand();
        public static RoutedUICommand CmdВ = new RoutedUICommand();
        public static RoutedUICommand CmdБ = new RoutedUICommand();
        public static RoutedUICommand CmdН = new RoutedUICommand();
        public static RoutedUICommand CmdМ = new RoutedUICommand();
        public static RoutedUICommand CmdGreaterThan = new RoutedUICommand();
        public static RoutedUICommand CmdLessThan = new RoutedUICommand();
        public static RoutedUICommand CmdQuestion = new RoutedUICommand();



        public static RoutedUICommand CmdSpaceBar = new RoutedUICommand();
        public static RoutedUICommand CmdClear = new RoutedUICommand();
        public static RoutedUICommand CmdClose = new RoutedUICommand();



        public TouchScreenKeyboard()
        {
            this.Width = WidthTouchKeyboard;
            this.Height = HeightTouchKeyboard;

        }

       

        static TouchScreenKeyboard()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(TouchScreenKeyboard), new FrameworkPropertyMetadata(typeof(TouchScreenKeyboard)));

            SetCommandBinding();
        }
        #endregion
        #region CommandRelatedCode
        private static void SetCommandBinding()
        {
            CommandBinding CbTlide = new CommandBinding(CmdTlide, RunCommand);
            CommandBinding Cb1 = new CommandBinding(Cmd1, RunCommand);
            CommandBinding Cb2 = new CommandBinding(Cmd2, RunCommand);
            CommandBinding Cb3 = new CommandBinding(Cmd3, RunCommand);
            CommandBinding Cb4 = new CommandBinding(Cmd4, RunCommand);
            CommandBinding Cb5 = new CommandBinding(Cmd5, RunCommand);
            CommandBinding Cb6 = new CommandBinding(Cmd6, RunCommand);
            CommandBinding Cb7 = new CommandBinding(Cmd7, RunCommand);
            CommandBinding Cb8 = new CommandBinding(Cmd8, RunCommand);
            CommandBinding Cb9 = new CommandBinding(Cmd9, RunCommand);
            CommandBinding Cb0 = new CommandBinding(Cmd0, RunCommand);
            CommandBinding CbMinus = new CommandBinding(CmdMinus, RunCommand);
            CommandBinding CbPlus = new CommandBinding(CmdPlus, RunCommand);
            CommandBinding CbBackspace = new CommandBinding(CmdBackspace, RunCommand);

            CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbTlide);
            CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), Cb1);
            CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), Cb2);
            CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), Cb3);
            CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), Cb4);
            CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), Cb5);
            CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), Cb6);
            CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), Cb7);
            CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), Cb8);
            CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), Cb9);
            CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), Cb0);
            CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbMinus);
            CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbPlus);
            CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbBackspace);


            CommandBinding CbTab = new CommandBinding(CmdTab, RunCommand);
            CommandBinding CbЉ = new CommandBinding(CmdЉ, RunCommand);
            CommandBinding CbЊ = new CommandBinding(CmdЊ, RunCommand);
            CommandBinding CbЕ = new CommandBinding(CmdЕ, RunCommand);
            CommandBinding CbР = new CommandBinding(CmdР, RunCommand);
            CommandBinding CbТ = new CommandBinding(CmdТ, RunCommand);
            CommandBinding CbЅ = new CommandBinding(CmdЅ, RunCommand);
            CommandBinding CbУ = new CommandBinding(CmdУ, RunCommand);
            CommandBinding CbИ = new CommandBinding(CmdИ, RunCommand);
            CommandBinding CbО = new CommandBinding(CmdО, RunCommand);
            CommandBinding CbП = new CommandBinding(CmdП, RunCommand);
            CommandBinding CbШ = new CommandBinding(CmdШ, RunCommand);
            CommandBinding CbЃ = new CommandBinding(CmdЃ, RunCommand);
            CommandBinding CbЖ = new CommandBinding(CmdЖ, RunCommand);

            CommandBinding CbCapsLock = new CommandBinding(CmdCapsLock, RunCommand);
            CommandBinding CbА = new CommandBinding(CmdА, RunCommand);
            CommandBinding CbС = new CommandBinding(CmdС, RunCommand);
            CommandBinding CbД = new CommandBinding(CmdД, RunCommand);
            CommandBinding CbФ = new CommandBinding(CmdФ, RunCommand);
            CommandBinding CbГ = new CommandBinding(CmdГ, RunCommand);
            CommandBinding CbХ = new CommandBinding(CmdХ, RunCommand);
            CommandBinding CbЈ = new CommandBinding(CmdЈ, RunCommand);
            CommandBinding CbК = new CommandBinding(CmdК, RunCommand);
            CommandBinding CbЛ = new CommandBinding(CmdЛ, RunCommand);
            CommandBinding CbЧ = new CommandBinding(CmdЧ, RunCommand);
            CommandBinding CbЌ = new CommandBinding(CmdЌ, RunCommand);
            CommandBinding CbEnter = new CommandBinding(CmdEnter, RunCommand);

            CommandBinding CbShift = new CommandBinding(CmdShift, RunCommand);
            CommandBinding CbЗ = new CommandBinding(CmdЗ, RunCommand);
            CommandBinding CbЏ = new CommandBinding(CmdЏ, RunCommand);
            CommandBinding CbЦ = new CommandBinding(CmdЦ, RunCommand);
            CommandBinding CbВ = new CommandBinding(CmdВ, RunCommand);
            CommandBinding CbБ = new CommandBinding(CmdБ, RunCommand);
            CommandBinding CbН = new CommandBinding(CmdН, RunCommand);
            CommandBinding CbМ = new CommandBinding(CmdМ, RunCommand);
            CommandBinding CbGreaterThan = new CommandBinding(CmdGreaterThan, RunCommand);
            CommandBinding CbLessThan = new CommandBinding(CmdLessThan, RunCommand);
            CommandBinding CbQuestion = new CommandBinding(CmdQuestion, RunCommand);



            CommandBinding CbSpaceBar = new CommandBinding(CmdSpaceBar, RunCommand);
            CommandBinding CbClear = new CommandBinding(CmdClear, RunCommand);
            CommandBinding CbClose = new CommandBinding(CmdClose, RunCommand);

            CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbTab);
            CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbЉ);
            CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbЊ);
            CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbЕ);
            CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbР);
            CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbТ);
            CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbЅ);
            CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbУ);
            CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbИ);
            CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbО);
            CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbП);
            CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbШ);
            CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbЃ);
            CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbЖ);

            CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbCapsLock);
            CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbА);
            CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbС);
            CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbД);
            CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbФ);
            CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbГ);
            CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbХ);
            CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbЈ);
            CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbК);
            CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbЛ);
            CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbЧ);
            CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbЌ);
            CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbEnter);

            CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbShift);
            CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbЗ);
            CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbЏ);
            CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbЦ);
            CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbВ);
            CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbБ);
            CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbН);
            CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbМ);
            CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbGreaterThan);
            CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbLessThan);
            CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbQuestion);



            CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbSpaceBar);
            CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbClear);
            CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbClose);

        }
        static void RunCommand(object sender, ExecutedRoutedEventArgs e)
        {

            if (e.Command == CmdTlide)  //First Row
            {


                if (!ShiftFlag)
                {
                    TouchScreenKeyboard.TouchScreenText += "`";
                }
                else
                {
                    TouchScreenKeyboard.TouchScreenText += "~";
                    ShiftFlag = false;
                }
            }
            else if (e.Command == Cmd1)
            {
                if (!ShiftFlag)
                {
                    TouchScreenKeyboard.TouchScreenText += "1";
                }
                else
                {
                    TouchScreenKeyboard.TouchScreenText += "!";
                    ShiftFlag = false;
                }

            }
            else if (e.Command == Cmd2)
            {
                if (!ShiftFlag)
                {
                    TouchScreenKeyboard.TouchScreenText += "2";
                }
                else
                {
                    TouchScreenKeyboard.TouchScreenText += "@";
                    ShiftFlag = false;
                }

            }
            else if (e.Command == Cmd3)
            {
                if (!ShiftFlag)
                {
                    TouchScreenKeyboard.TouchScreenText += "3";
                }
                else
                {
                    TouchScreenKeyboard.TouchScreenText += "#";
                    ShiftFlag = false;
                }

            }
            else if (e.Command == Cmd4)
            {
                if (!ShiftFlag)
                {
                    TouchScreenKeyboard.TouchScreenText += "4";
                }
                else
                {
                    TouchScreenKeyboard.TouchScreenText += "$";
                    ShiftFlag = false;
                }

            }
            else if (e.Command == Cmd5)
            {
                if (!ShiftFlag)
                {
                    TouchScreenKeyboard.TouchScreenText += "5";
                }
                else
                {
                    TouchScreenKeyboard.TouchScreenText += "%";
                    ShiftFlag = false;
                }

            }
            else if (e.Command == Cmd6)
            {
                if (!ShiftFlag)
                {
                    TouchScreenKeyboard.TouchScreenText += "6";
                }
                else
                {
                    TouchScreenKeyboard.TouchScreenText += "^";
                    ShiftFlag = false;
                }

            }
            else if (e.Command == Cmd7)
            {
                if (!ShiftFlag)
                {
                    TouchScreenKeyboard.TouchScreenText += "7";
                }
                else
                {
                    TouchScreenKeyboard.TouchScreenText += "&";
                    ShiftFlag = false;
                }

            }
            else if (e.Command == Cmd8)
            {
                if (!ShiftFlag)
                {
                    TouchScreenKeyboard.TouchScreenText += "8";
                }
                else
                {
                    TouchScreenKeyboard.TouchScreenText += "*";
                    ShiftFlag = false;
                }

            }
            else if (e.Command == Cmd9)
            {
                if (!ShiftFlag)
                {
                    TouchScreenKeyboard.TouchScreenText += "9";
                }
                else
                {
                    TouchScreenKeyboard.TouchScreenText += "(";
                    ShiftFlag = false;
                }

            }
            else if (e.Command == Cmd0)
            {
                if (!ShiftFlag)
                {
                    TouchScreenKeyboard.TouchScreenText += "0";
                }
                else
                {
                    TouchScreenKeyboard.TouchScreenText += ")";
                    ShiftFlag = false;
                }

            }
            else if (e.Command == CmdMinus)
            {
                if (!ShiftFlag)
                {
                    TouchScreenKeyboard.TouchScreenText += "-";
                }
                else
                {
                    TouchScreenKeyboard.TouchScreenText += "_";
                    ShiftFlag = false;
                }

            }
            else if (e.Command == CmdPlus)
            {
                if (!ShiftFlag)
                {
                    TouchScreenKeyboard.TouchScreenText += "=";
                }
                else
                {
                    TouchScreenKeyboard.TouchScreenText += "+";
                    ShiftFlag = false;
                }

            }
            else if (e.Command == CmdBackspace)
            {
                if (!string.IsNullOrEmpty(TouchScreenKeyboard.TouchScreenText))
                {
                    TouchScreenKeyboard.TouchScreenText = TouchScreenKeyboard.TouchScreenText.Substring(0, TouchScreenKeyboard.TouchScreenText.Length - 1);
                }

            }
            else if (e.Command == CmdTab)  //Second Row
            {
                TouchScreenKeyboard.TouchScreenText += "    ";
            }
            else if (e.Command == CmdЉ)
            {
                AddKeyBoardINput('Љ');
            }
            else if (e.Command == CmdЊ)
            {
                AddKeyBoardINput('Њ');
            }
            else if (e.Command == CmdЕ)
            {
                AddKeyBoardINput('Е');
            }
            else if (e.Command == CmdР)
            {
                AddKeyBoardINput('Р');
            }
            else if (e.Command == CmdТ)
            {
                AddKeyBoardINput('Т');
            }
            else if (e.Command == CmdЅ)
            {
                AddKeyBoardINput('Ѕ');
            }
            else if (e.Command == CmdУ)
            {
                AddKeyBoardINput('У');

            }
            else if (e.Command == CmdИ)
            {
                AddKeyBoardINput('И');
            }
            else if (e.Command == CmdО)
            {
                AddKeyBoardINput('О');
            }
            else if (e.Command == CmdП)
            {
                AddKeyBoardINput('П');
            }
            else if (e.Command == CmdШ)
            {
                AddKeyBoardINput('Ш');
            }
            else if (e.Command == CmdЃ)
            {
                AddKeyBoardINput('Ѓ');
            }
            else if (e.Command == CmdЖ)
            {
                AddKeyBoardINput('Ж');
            }
            else if (e.Command == CmdCapsLock)  ///Third ROw
            {

                if (!CapsLockFlag)
                {
                    CapsLockFlag = true;
                }
                else
                {
                    CapsLockFlag = false;

                }
            }
            else if (e.Command == CmdА)
            {
                AddKeyBoardINput('А');
            }
            else if (e.Command == CmdС)
            {
                AddKeyBoardINput('С');
            }
            else if (e.Command == CmdД)
            {
                AddKeyBoardINput('Д');
            }
            else if (e.Command == CmdФ)
            {
                AddKeyBoardINput('Ф');
            }
            else if (e.Command == CmdГ)
            {
                AddKeyBoardINput('Г');
            }
            else if (e.Command == CmdХ)
            {
                AddKeyBoardINput('Х');
            }
            else if (e.Command == CmdЈ)
            {
                AddKeyBoardINput('Ј');
            }
            else if (e.Command == CmdК)
            {
                AddKeyBoardINput('К');
            }
            else if (e.Command == CmdЛ)
            {
                AddKeyBoardINput('Л');

            }
            else if (e.Command == CmdЧ)
            {
                AddKeyBoardINput('Ч');

            }
            else if (e.Command == CmdЌ)
            {
                AddKeyBoardINput('Ќ');

            }
            else if (e.Command == CmdEnter)
            {
                if (_InstanceObject != null)
                {
                    _InstanceObject.Close();
                    _InstanceObject = null;
                }
                _CurrentControl.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));


            }
            else if (e.Command == CmdShift) //Fourth Row
            {

                ShiftFlag = true; ;


            }
            else if (e.Command == CmdЗ)
            {
                AddKeyBoardINput('З');

            }
            else if (e.Command == CmdЏ)
            {
                AddKeyBoardINput('Џ');

            }
            else if (e.Command == CmdЦ)
            {
                AddKeyBoardINput('Ц');

            }
            else if (e.Command == CmdВ)
            {
                AddKeyBoardINput('В');

            }
            else if (e.Command == CmdБ)
            {
                AddKeyBoardINput('Б');

            }
            else if (e.Command == CmdН)
            {
                AddKeyBoardINput('Н');

            }
            else if (e.Command == CmdМ)
            {
                AddKeyBoardINput('М');

            }
            else if (e.Command == CmdLessThan)
            {
                if (!ShiftFlag)
                {
                    TouchScreenKeyboard.TouchScreenText += ",";
                }
                else
                {
                    TouchScreenKeyboard.TouchScreenText += ";";
                    ShiftFlag = false;
                }

            }
            else if (e.Command == CmdGreaterThan)
            {
                if (!ShiftFlag)
                {
                    TouchScreenKeyboard.TouchScreenText += ".";
                }
                else
                {
                    TouchScreenKeyboard.TouchScreenText += ":";
                    ShiftFlag = false;
                }

            }
            else if (e.Command == CmdQuestion)
            {
                if (!ShiftFlag)
                {
                    TouchScreenKeyboard.TouchScreenText += "/";
                }
                else
                {
                    TouchScreenKeyboard.TouchScreenText += "?";
                    ShiftFlag = false;
                }

            }
            else if (e.Command == CmdSpaceBar)//Last row
            {

                TouchScreenKeyboard.TouchScreenText += " ";
            }
            else if (e.Command == CmdClear)//Last row
            {

                TouchScreenKeyboard.TouchScreenText = "";
              
            }
            else if (e.Command == CmdClose)//Last row
            {
                FrameworkElement host = sender as FrameworkElement;
                
                   
                   
                
                    _InstanceObject.Close();
                    _InstanceObject = null;
                   
           
            }
        }
        #endregion
        #region Main Functionality
        private static void AddKeyBoardINput(char input)
        {
            if (CapsLockFlag)
            {
                if (ShiftFlag)
                {
                    TouchScreenKeyboard.TouchScreenText += char.ToLower(input).ToString();
                    ShiftFlag = false;

                }
                else
                {
                    TouchScreenKeyboard.TouchScreenText += char.ToUpper(input).ToString();
                }
            }
            else
            {
                if (!ShiftFlag)
                {
                    TouchScreenKeyboard.TouchScreenText += char.ToLower(input).ToString();
                }
                else
                {
                    TouchScreenKeyboard.TouchScreenText += char.ToUpper(input).ToString();
                    ShiftFlag = false;
                }
            }
        }


        private static void syncchild()
        {
            if (_CurrentControl != null && _InstanceObject != null)
            {

               /* Point virtualpoint = new Point(0, _CurrentControl.ActualHeight + 3);
                Point Actualpoint = _CurrentControl.PointToScreen(virtualpoint);

                if (WidthTouchKeyboard + Actualpoint.X > SystemParameters.VirtualScreenWidth)
                {
                    double difference = WidthTouchKeyboard + Actualpoint.X - SystemParameters.VirtualScreenWidth;
                    _InstanceObject.Left = Actualpoint.X - difference;
                }
                else if (!(Actualpoint.X > 1))
                {
                    _InstanceObject.Left = 1;
                }
                else
                    _InstanceObject.Left = Actualpoint.X;




                _InstanceObject.Top = Actualpoint.Y;
                _InstanceObject.Show();*/

                _InstanceObject.Left = 170;




                _InstanceObject.Top = 420;
                _InstanceObject.Show();
            }


        }

        public static bool GetTouchScreenKeyboard(DependencyObject obj)
        {
            return (bool)obj.GetValue(TouchScreenKeyboardProperty);
        }

        public static void SetTouchScreenKeyboard(DependencyObject obj, bool value)
        {
            obj.SetValue(TouchScreenKeyboardProperty, value);
        }

        public static readonly DependencyProperty TouchScreenKeyboardProperty =
            DependencyProperty.RegisterAttached("TouchScreenKeyboard", typeof(bool), typeof(TouchScreenKeyboard), new UIPropertyMetadata(default(bool), TouchScreenKeyboardPropertyChanged));



        static void TouchScreenKeyboardPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            FrameworkElement host = sender as FrameworkElement;
            if (host != null)
            {
                host.GotFocus += new RoutedEventHandler(OnGotFocus);
                host.LostFocus += new RoutedEventHandler(OnLostFocus);
            }

        }



        static void OnGotFocus(object sender, RoutedEventArgs e)
        {
            Control host = sender as Control;

            _PreviousTextBoxBackgroundBrush = host.Background;
            _PreviousTextBoxBorderBrush = host.BorderBrush;
            _PreviousTextBoxBorderThickness = host.BorderThickness;

            host.Background = Brushes.White;
            host.BorderBrush = Brushes.LightBlue;
            host.BorderThickness = new Thickness(4);


            _CurrentControl = host;

            if (_InstanceObject == null)
            {
                FrameworkElement ct = host;
                while (true)
                {
                    if (ct is Window)
                    {
                        ((Window)ct).LocationChanged += new EventHandler(TouchScreenKeyboard_LocationChanged);
                        ((Window)ct).Activated += new EventHandler(TouchScreenKeyboard_Activated);
                        ((Window)ct).Deactivated += new EventHandler(TouchScreenKeyboard_Deactivated);
                        
                        break;
                    }
                    ct = (FrameworkElement)ct.Parent;
                }

                _InstanceObject = new TouchScreenKeyboard();
                _InstanceObject.AllowsTransparency = true;
                _InstanceObject.WindowStyle = WindowStyle.None;
                _InstanceObject.ShowInTaskbar = false;
                _InstanceObject.ShowInTaskbar = false;
                _InstanceObject.Topmost = true;

                host.LayoutUpdated += new EventHandler(tb_LayoutUpdated);
            }



        }

        static void TouchScreenKeyboard_Deactivated(object sender, EventArgs e)
        {
            if (_InstanceObject != null)
            {
                _InstanceObject.Topmost = false;
            }
        }

        static void TouchScreenKeyboard_Activated(object sender, EventArgs e)
        {
            if (_InstanceObject != null)
            {
                _InstanceObject.Topmost = true;
            }
        }



        static void TouchScreenKeyboard_LocationChanged(object sender, EventArgs e)
        {
            syncchild();
        }

        static void tb_LayoutUpdated(object sender, EventArgs e)
        {
            syncchild();
        }



        static void OnLostFocus(object sender, RoutedEventArgs e)
        {

            Control host = sender as Control;
            host.Background = _PreviousTextBoxBackgroundBrush;
            host.BorderBrush = _PreviousTextBoxBorderBrush;
            host.BorderThickness = _PreviousTextBoxBorderThickness;

            if (_InstanceObject != null)
            {
                _InstanceObject.Close();
                _InstanceObject = null;
            }



        }

        #endregion
    }
}
